package com.example.code2022;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author: dangwen
 * @createDate: 2022/4/23
 */
public class Test {
    public static void main(String[] args) {

        int[] arr = {-2,1,-3,4,-1,2,1,-5,4};
//        System.out.println(JSON.toJSONString(Arrays.copyOfRange(arr, 1, 3)));
        System.out.println(JSON.toJSONString(findSubArr(arr)));
    }



    public static int[] findSubArr(int[] nums){
        int max = Integer.MIN_VALUE;
        int carry = 0;
        int right = -1;
        int left = -1;
        for (int i = 0; i < nums.length; i++) {
            if (carry >= 0){
                carry += nums[i];
            } else {
                carry = nums[i];
            }
            if (carry > max){
                max = carry;
                right = i;
            }
//            max = Math.max(carry, max);
        }
        for (int j = right; j >= 0 ; j--) {
            max = max - nums[j];
            if (max == 0){
                left = j;
            }
        }
        return Arrays.copyOfRange(nums,left,right + 1);
    }
}
